package com.example.sample.list.data;

import com.example.sample.*;
import ohos.aafwk.ability.Ability;
import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import java.util.ArrayList;
import java.util.Arrays;

public class ListItemProvider extends RecycleItemProvider {
    private ArrayList<SampleConfig> data = new ArrayList<>();
    private Ability mSlice;

    ListItemProvider(Ability abilitySlice) {
        mSlice = abilitySlice;
        data.addAll(Arrays.asList(samplesConfig));
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        DirectionalLayout layout = new DirectionalLayout(mSlice);
        layout.setOrientation(DirectionalLayout.VERTICAL);

        Text text = new Text(mSlice);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(RgbPalette.parse("#FFDFDFDF")));
        text.setBackground(element);
        text.setPadding(20, 10, 20, 10);
        text.setTextSize(30, Text.TextSizeType.FP);
        text.setText(data.get(position).titleId);

        final int matchParent = ComponentContainer.LayoutConfig.MATCH_PARENT;
        final int matchContent = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(matchParent, matchContent);
        layout.addComponent(text, layoutConfig);

        Text divider = new Text(mSlice);
        ShapeElement dividerElement = new ShapeElement();
        dividerElement.setRgbColor(RgbColor.fromArgbInt(RgbPalette.parse("#00000000")));
        divider.setBackground(dividerElement);
        ComponentContainer.LayoutConfig dividerLayoutConfig = new ComponentContainer.LayoutConfig(matchParent, matchContent);
        dividerLayoutConfig.height = 10;
        layout.addComponent(divider, dividerLayoutConfig);

        layout.setLayoutConfig(layoutConfig);

        return layout;
    }

    private static final SampleConfig[] samplesConfig = new SampleConfig[]{
            new SampleConfig(ResourceTable.String_title_get_sample, GetSample.class),
            new SampleConfig(ResourceTable.String_title_post_sample, PostSample.class),
            new SampleConfig(ResourceTable.String_title_json_sample, JsonSample.class),
            new SampleConfig(ResourceTable.String_title_network_image_view, NetworkImageViewAbility.class),
            new SampleConfig(ResourceTable.String_title_imageLoader,ImageLoaderAbility.class)
            /*,
            new SampleConfig(ResourceTable.String_title_delete_sample, DeleteSample.class),
            new SampleConfig(ResourceTable.String_title_put_sample, PutSample.class),
            new SampleConfig(ResourceTable.String_title_patch_sample, PatchSample.class),
            new SampleConfig(ResourceTable.String_title_head_sample, HeadSample.class),
            new SampleConfig(ResourceTable.String_title_json_streamer_sample, JsonStreamerSample.class),
            new SampleConfig(ResourceTable.String_title_sax_example, SaxSample.class),
            new SampleConfig(ResourceTable.String_title_file_sample, FileSample.class),
            new SampleConfig(ResourceTable.String_title_directory_sample, DirectorySample.class),
            new SampleConfig(ResourceTable.String_title_binary_sample, BinarySample.class),
            new SampleConfig(ResourceTable.String_title_gzip_sample, GzipSample.class),
            new SampleConfig(ResourceTable.String_title_redirect_302, Redirect302Sample.class),
            new SampleConfig(ResourceTable.String_title_threading_timeout, ThreadingTimeoutSample.class),
            new SampleConfig(ResourceTable.String_title_cancel_all, CancelAllRequestsSample.class),
            new SampleConfig(ResourceTable.String_title_cancel_handle, CancelRequestHandleSample.class),
            new SampleConfig(ResourceTable.String_title_cancel_tag, CancelRequestByTagSample.class),
            new SampleConfig(ResourceTable.String_title_synchronous, SynchronousClientSample.class),
            new SampleConfig(ResourceTable.String_title_intent_service_sample, IntentServiceSample.class),
            new SampleConfig(ResourceTable.String_title_post_files, FilesSample.class),
            new SampleConfig(ResourceTable.String_title_persistent_cookies, PersistentCookiesSample.class),
            new SampleConfig(ResourceTable.String_title_custom_ca, CustomCASample.class),
            new SampleConfig(ResourceTable.String_title_retry_handler, RetryRequestSample.class),
            new SampleConfig(ResourceTable.String_title_range_sample, RangeResponseSample.class),
            new SampleConfig(ResourceTable.String_title_401_unauth, Http401AuthSample.class),
            new SampleConfig(ResourceTable.String_title_pre_post_processing, PrePostProcessingSample.class),
            new SampleConfig(ResourceTable.String_title_content_type_http_entity, ContentTypeForHttpEntitySample.class),
            new SampleConfig(ResourceTable.String_title_resume_download, ResumeDownloadSample.class),
            new SampleConfig(ResourceTable.String_title_digest_auth, DigestAuthSample.class),
            new SampleConfig(ResourceTable.String_title_use_pool_thread, UsePoolThreadSample.class),
            new SampleConfig(ResourceTable.String_title_request_params_debug, RequestParamsDebug.class)*/
    };

    static class SampleConfig {
        final int titleId;
        final Class targetClass;
        SampleConfig(int titleId, Class targetClass) {
            this.titleId = titleId;
            this.targetClass = targetClass;
        }
    }
}
